package com.msb.servicedriveuser.service.impl;

import com.msb.commons.dto.Car;
import com.msb.commons.response.ResponseResult;
import com.msb.commons.response.TerminalResponse;
import com.msb.commons.response.TrackResponse;
import com.msb.servicedriveuser.feign.TerminalClient;
import com.msb.servicedriveuser.mapper.CarMapper;
import com.msb.servicedriveuser.service.CarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chshh
 * @since 2022-10-25
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {
    @Resource
    private CarMapper carMapper;

    @Autowired
    private TerminalClient terminalClient;

    @Override
    public ResponseResult<Car> getCarById(Long carId) {
        Car car = carMapper.selectById(carId);
        return ResponseResult.success(car);
    }

    @Override
    public ResponseResult addCar(Car car) {
        LocalDateTime now = LocalDateTime.now();
        car.setGmtCreate(now);
        car.setGmtModified(now);
        //保存车辆
        carMapper.insert(car);

        //获得此车辆的终端id:tid
        ResponseResult<TerminalResponse> responseResult = terminalClient.addTerminal(car.getVehicleNo(),String.valueOf(car.getId()));
        String tid = responseResult.getData().getTid();
        car.setTid(tid);
        //获得此车辆的轨迹id:trid
        ResponseResult<TrackResponse> track = terminalClient.addTrack(tid);
        String trid = track.getData().getTrid();
        String trname = track.getData().getTrname();
        car.setTrid(trid);
        car.setTrname(trname);

        carMapper.updateById(car);
        return ResponseResult.success("");
    }
}
